<template>
	<view class="user_setting_bind">
		<view class="identityTwo" v-if="type==1">
			<view class="list hi-rows">
				<text>手机号码</text>
				<input type="text" v-model="info.phone" placeholder="手机号码"  placeholder-style="color:#D7D7D7" />
			</view>
			<view class="list  hi-rows">
				<text>验证码</text>
				<input type="text" v-model="info.code" placeholder="验证码"   placeholder-style="color:#D7D7D7" />
				<text @click="sendSms()">{{text}}</text>
			</view>
			
		</view>
		<view class="viewBtn" @tap="checkBind()" v-if="type==1">完成绑定</view>
		<!-- #ifdef APP-PLUS -->
		<view class="viewBtn" @tap="checkBind2()" v-if="type==0">绑定微信</view>
		<!-- #endif -->
		<!-- #ifdef H5 -->
		<view class="viewBtn" @tap="checkBind2()" v-if="type==0">绑定微信</view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type:0,
				num:0,
				wait:false,
				text:'发送验证码',
				info:{},
			};
		},
		methods:{
			sendSms(){
				if(this.wait){
					return this.$toast('请不要频繁发送短信')
				}
				this.$http(this.$api.sendSms,{phone:this.info.phone,name:'edit_user'}).then(res=>{
					if(res.code==200){
						this.num = 60;
						this.wait = true;
						let obj = setInterval(()=>{
							this.num--
							this.text = this.num+'s';
							if(this.num<1){
								clearInterval(obj);
								this.num = 0
								this.text = '发送验证码';
							}
						},1000);
					}else{
						return this.$toast(res.msg)
					}
				})
			},
			checkBind(){
				let obj2 = {...this.$api.users}
				obj2.url = obj2.url+'/edit_user'
				obj2.method = 'put'
				this.$http(obj2,this.info).then(res=>{
					uni.$emit('users')
					this.$toast(res.msg);
					return this.$nav('',3)
				})
			},
			checkBind2(){
					
				// #ifdef H5
				console.log(this.rootUrl + "oauth/weixin")
				return window.location.href=this.$api.rootUrls + "oauth/weixin"
				// #endif
				
				// return this.$toast('暂未开放');
				uni.login({
				  provider: 'weixin',
				  success:  (loginRes)=> {
					// console.log(loginRes.authResult);
					// 获取用户信息
					uni.getUserInfo({
					  provider: 'weixin',
					  success:  (infoRes)=> {
						loginRes.authResult.nickname = infoRes.userInfo.nickName
						loginRes.authResult.avatar = infoRes.userInfo.avatarUrl
						
						let obj2 = {...this.$api.users}
						obj2.url = obj2.url+'/edit_user'
						obj2.method = 'put'
						this.$http(obj2,{oauth:JSON.stringify(loginRes.authResult)}).then(res=>{
							console.log(res)
							uni.$emit('users')
							this.$toast(res.msg);
							return this.$nav('',3)
						})
						
					  }
					});
				  }
				});
			}
		},
		onLoad(opt) {
			this.type = opt.type;
		}
	}
</script>

<style lang="scss" scoped>
view,
button,
image {
	box-sizing: border-box;
}
.user_setting_bind{
	.identityTwo {
		background-color: #fff;
		padding-left: 52rpx;
	}
	
	.identityTwo .list text {
		color: #222222;
		font-size: 30rpx;
		width: 150rpx;
		float: left;
	}
	
	.identityTwo .list input{
		flex: 1;
		float: left;
		width: 300rpx;
		margin-top: 24rpx;
	}
	.check_content {
		border-top: 1px solid #efefef;
		width: 100%;
		display: flex;
		flex-direction: column;
		height: 100%;
	}
	
	.flex {
		display: flex;
		display: -webkit-flex;
		justify-content: space-between;
		flex-direction: row;
		flex-wrap: wrap;
	}
	
	.idTwo .upload image{
		width: 132rpx;
		height: 180rpx;
	}
	.idTwo image {
		width: 132rpx;
		height: 180rpx;
	}

	.idTwo view {
		width: 100%;
		height: 48rpx;
		font-size: 26rpx;
		font-weight: 400;
		color: rgba(102, 102, 102, 1);
		line-height: 48rpx;
		text-align: center;
		margin-top: 18rpx;
	}

	.idTwo view text {
		color: #FF5C50;
		margin-left: 5rpx;
	}

	.identityTwo {
		margin-top: 26rpx;
	}

	.list {
		height: 100rpx;
		line-height: 100rpx;
		align-items: center;
		justify-content: flex-start;
	}

	.list:nth-of-type(1) {
		border-bottom: 1rpx solid rgba(248, 248, 248, 1);
	}

	.list input {
		font-size: 30rpx;
		padding-left: 70rpx;
	}


	.viewBtn {
		width: 638rpx;
		height: 88rpx;
		line-height: 88rpx;
		color: #fff;
		background-color: #ea1f22;
		border-radius: 10rpx;
		box-shadow: 1px 2px 5px rgba(219, 40, 43, 0.4);	
		font-size: 30rpx;
		text-align: center;
		margin: 0 auto;
		margin-top: 100rpx;
	}
}
</style>
